package com.rjyb.student_management_platform.controller;

import com.rjyb.student_management_platform.pojo.Student;
import com.rjyb.student_management_platform.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.Calendar;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService us;

    //学生个人查询信息
    @RequestMapping("/query")
    public String query(Model model,HttpSession session){
        Map student = us.queryStudent(session);
        model.addAttribute("user",student);
        return "student/lyear_pages_profile";
    }
    //去学生注册
    @RequestMapping("/stuenllor")
    public String  stuenllor(){
        return "student/lyear_pages_enroll";
    }
    //学生注册
    @RequestMapping("/enllor")
    public String enllor(Student student, String studentId, String iname){
        Calendar calendar=Calendar.getInstance();
        student.setName(iname);
        Student student1 = us.ifStudent(studentId, iname);
        //算生日
        student.setAge(Integer.toString((calendar.get(Calendar.YEAR)-Integer.valueOf(student.getBirthday().substring(0,4)))));
        if ((student1!=null)){
            System.out.println("密码是"+us.ifStudent(studentId,iname).getPassword());
            if(!student1.getPassword().equals("未添加")&&!student1.getIdCard().equals("未添加")){

                return "student/enroll_no";
                //已注册
            }else {
                System.out.println(student);
                us.addStudent(student);
                return "redirect:/login/tologin";}
        }else {
            //注册不了
            return "student/enroll_norun";
        }


    }

    //去学生修改密码
    @RequestMapping("/toupdatastupassword")
    public String toupdatastupassword (){
        return "student/updatastupassword";
    }

    //去老师修改密码
    @RequestMapping("/toupdatateapassword")
    public String toupdatateapassword (){
        return "teacher/updatateapassword";
    }

    //修改密码
    @RequestMapping("/updatapassword")
    public String updatapassword(String password, String password1, HttpSession session, Model model){
        return us.updatapassword(password,password1,session,model);
    }

}
